/*
 * $Id: ResourceType.java,v 1.8 2010/04/01 14:26:23 navdeep_mahajan Exp $
 *
 * Copyright (C) 2006 General Electric Company. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 *
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */

package com.ge.healthcare.autosc.common.load;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import com.ge.healthcare.autosc.common.database.pojo.IActTask;
/**
 * System ResourceLoadType is queue that allows ITasks to be consumed and released from a queue
 * @author 212028693
 *
 */
public class ResourceType implements IResourceType {
	/**
	 * Eclipse generated
	 */
	private static final long serialVersionUID = 3604047135596665677L;
	private int max;
	private int expirationDuration;
	private Map<Long, IActTask> current;

	public ResourceType(int max, int expirationDuration) {
		this.max = max;
		this.expirationDuration = expirationDuration;
		current = new HashMap<Long, IActTask>();
	}

	public ResourceType(int max, int expirationDuration, List<IActTask> contents) {
		this.max = max;
		this.expirationDuration = expirationDuration;
		current = new HashMap<Long, IActTask>();
		for (IActTask task : contents) {
			this.current.put(task.getTaskId(), task);
		}
	}

	/**
	 * Consume a resource if possible
	 */
	public boolean consume(IActTask task) {
		boolean wasConsumed = false;
		//Consume if size is less then max AND not already contained
		if ((current.size() + 1) <= max && !current.containsKey(task.getTaskId())) {
			//Can't set the queue time here as it is called by scheduler every time
			//task.setQueuedTime(new Date());
			current.put(task.getTaskId(), task);
			wasConsumed = true;
		}
		return wasConsumed;
	}

	/**
	 * release a consumed resource
	 */
	public void release(IActTask task) {
		if (current.containsKey(task.getTaskId()) ) {
			current.remove(task.getTaskId());
		}
	}

	public boolean contains(IActTask task) {
		return current.containsKey(task.getTaskId());
	}

	public boolean contains(Long taskId) {
		return current.containsKey(taskId);
	}

	public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append("{Max Value = ").append(max).append("},");
		sb.append("{Current Value = ").append(current).append("},");
		sb.append("{Expiration Duration = ").append(expirationDuration).append("}");
		return sb.toString();
	}

	/**
	 * @return the max
	 */
	public int getMax() {
		return max;
	}

	/**
	 * @return the current size of the queue
	 */
	public int getCurrentSize() {
		return current.size();
	}
	
	public Set<Long> getKeys(){
		Map<Long, IActTask> treeMap = new TreeMap<Long, IActTask>(current);
		return treeMap.keySet();
	}
	public IActTask get(Long key) {
		return current.get(key);
	}

	/**
	 * @return the expirationDuration
	 */
	public int getExpirationDuration() {
		return expirationDuration;
	}

	/**
	 * @param expirationDuration the expirationDuration to set
	 */
	public void setExpirationDuration(int expirationDuration) {
		this.expirationDuration = expirationDuration;
	}
}
